/**
 考试（竞赛）列表
*/
<template>
    <div>
        <div v-if="indexKey === 1">
            <condition-card
                @reset="reset"
                @search="onSubmit"
                label-position="right"
                label-width="150px"
            >
                <el-row :gutter="24" style="height: 50px">
                    <!-- 竞赛标题 -->
                    <el-col :span="8">
                        <el-form-item :label="$t('竞赛标题')">
                            <el-input
                                v-model="queryForm.condition.projectName"
                                :placeholder="$t('请输入')"
                                clearable
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <!-- 年度  -->
                    <el-col :span="8">
                        <el-form-item :label="$t('年度')" prop="year">
                            <el-date-picker
                                :placeholder="$t('scoringItemSetting.pleaseSelect')"
                                style="width: 100%"
                                v-model="queryForm.condition.year"
                                type="year"
                                value-format="yyyy"
                                format="yyyy"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <!-- 竞赛开始时间 -->
                    <!-- <el-col :span="8">
                        <el-form-item :label="$t('竞赛开始时间')" size="small">
                            <el-date-picker
                                :placeholder="$t('academicForum.pleaseSelect')"
                                style="width: 100%"
                                v-model="queryForm.condition.signStartStr"
                                :picker-options="pickerOptionsStart"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col> -->

                    <!-- 竞赛结束时间 -->
                    <!-- <el-col :span="8">
                        <el-form-item :label="$t('竞赛结束时间')" size="small">
                            <el-date-picker
                                :placeholder="$t('academicForum.pleaseSelect')"
                                style="width: 100%"
                                v-model="queryForm.condition.signEndStr"
                                :picker-options="pickerOptionsEnd"
                                type="date"
                                value-format="yyyy-MM-dd"
                                format="yyyy-MM-dd"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col> -->
                </el-row>
            </condition-card>
            <el-card
                shadow="never"
                v-loading="loading"
                body-style="padding: 24px;"
                class="card-margin"
            >
                <h1 class="condition-title">{{ $t('竞赛列表') }}</h1>
                <!-- <div class="float-left associated-conditional-input">
                    <KeywordInput
                        v-model="queryForm.condition.projectNameOrId"
                        :placeholder="$t('academicForum.entryName')"
                        @enter="onSubmit"
                    />
                </div> -->
                <div class="edu-opertion-bar sub-title">
                    <div class="float-right" style="display: flex">
                        <el-button style="margin-left: 10px" type="primary" @click="exportFile">
                            {{ $t('internship.derive') }}
                        </el-button>
                        <el-button type="primary" @click="exportAdd">
                            {{ $t('academicForum.add') }}
                        </el-button>
                    </div>
                </div>
                <edu-table
                    :pagination="false"
                    :tooltipEffect="tooltipEffect"
                    :selection="false"
                    :order="false"
                    stripe
                    :data="dataList"
                    size="mini"
                    @selection-change="handleSelectionChange"
                >
                    <edu-table-column :label="$t('竞赛标题')" prop="projectName">
                        <template slot-scope="scope">
                            <el-button type="text" @click="viewCompetition(scope.row)">
                                {{ scope.row.projectName }}
                            </el-button>
                        </template>
                    </edu-table-column>
                    <!-- <el-table-column
                        :label="$t('examinationManagement.questionName')"
                        show-overflow-tooltip
                        min-width="100px"
                    >
                        <template slot-scope="scope">
                            <el-button type="text" @click="viewCompetition(scope.row)">
                                {{ scope.row.year }}
                            </el-button>
                        </template>
                    </el-table-column> -->
                    <edu-table-column :label="$t('年度')" prop="year"></edu-table-column>
                    <edu-table-column :label="$t('创建人')" prop="creator"></edu-table-column>
                    <edu-table-column :label="$t('创建人学院')" prop="createCollegeName"></edu-table-column>
                    <edu-table-column :label="$t('报名时间')" prop="signDate"></edu-table-column>
                    <edu-table-column :label="$t('考试时间')" prop="examDate"></edu-table-column>
                    <edu-table-column :label="$t('academicForum.operate')" width="500px">
                        <template slot-scope="props">
                            <el-button type="text" @click="editOutline(props.row)">
                                {{ $t('编辑') }}
                            </el-button>
                            <el-button type="text" @click="detailsOutline(props.row)"  v-if="props.row.hasSub === 0">
                                {{ $t('排考') }}
                            </el-button>
                            <el-button type="text" @click="printTheAdmissionCard(props.row)" v-if="props.row.hasSub === 0">
                                {{ $t('打印准考证') }}
                            </el-button>
                            <el-button type="text" @click="candidateInformationForm(props.row)" v-if="props.row.hasSub === 0">
                                {{ $t('考生信息表') }}
                            </el-button>
                            <el-button type="text" @click="printSignInForm(props.row)" v-if="props.row.hasSub === 0">
                                {{ $t('打印签到表') }}
                            </el-button>
                            <el-button type="text" @click="achievement(props.row)">
                                {{ $t('成绩管理') }}
                            </el-button>
                        </template>
                    </edu-table-column>
                </edu-table>
                <el-row type="flex" class="row-bg" justify="end">
                    <el-col>
                        <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="queryForm.pageNum_"
                            :page-sizes="[20, 40, 80, 100]"
                            :page-size="queryForm.pageSize_"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="queryForm.total_"
                            style="float: right; margin-top: 10px"
                        ></el-pagination>
                    </el-col>
                </el-row>
            </el-card>
            <edu-export-dialog
                :disabled="exportButton"
                :title="$t('message.prompt')"
                ref="downloadRef"
                :data="exportResult"
            ></edu-export-dialog>
        </div>
        <!-- 新增 -->
        <div v-if="indexKey === 2">
            <competitionListAdd
                @change-exam="changeExam"
                ref="addFinom"
                @change-add="changeAdd"
                :compile="compile"
                :dataList="dataListObj"
            ></competitionListAdd>
        </div>
        <!-- 排考 -->
        <div v-if="indexKey === 6">
            <competition-Arr
                :compile="compile"
                :dataObjList="dataObjList"
                :dataObjListTwo="dataObjListTwo"
                :jsId="jsId"
                :jsTitle="jsTitle"
                :examType="examType"
                :courseCode="courseCode"
                :dataObj="dataObj"
                :isArrange="isArrange"
                :mark="mark"
                @change-exam="changeExam"
            ></competition-Arr>
        </div>
        <!-- 成绩管理 -->
        <div v-if="indexKey === 5">
            <competitionManagement
                @change-exam="changeExam"
                ref="scoreFinom"
                :compile="compile"
                :dataInfo="dataListObj"
            ></competitionManagement>
        </div>
        <!-- 编辑 -->
        <div v-if="indexKey === 3">
            <competitionListEdit
                @change-exam="changeExam"
                :compile="compile"
                :editInfo="editInfo"
            ></competitionListEdit>
        </div>
        <!-- 查看 -->
        <div v-if="indexKey === 4">
            <competitionListView
                @change-exam="changeExam"
                :compile="compile"
                :dataList="dataListObj"
            ></competitionListView>
        </div>
    </div>
</template>
<script>
import { queryDic } from 'common/src/api/dictionary'
import UTILS from 'common/src/utils/utils'
import competitionListAdd from './competitionListAdd'
import competitionListEdit from './competitionListEdit'
import competitionListView from './competitionListView'
import competitionManagement from '../competitionManagement/competitionManagement'
import forumProject from 'common/src/api/academicForums/forumProject'
import competitionRuleApi from 'common/src/api/competitionRegistration/competitionRegistration'

import comRegistration from 'common/src/api/competitionRegistration/comRegistration'
import competitionListArrange from 'common/src/views/competitionRegistration/arrangingExaminations/arrangingExaminations'
export default {
    components: {
        competitionListAdd,
        competitionListEdit,
        competitionListView,
        'competition-Arr': competitionListArrange,
        competitionManagement
    },
    data() {
        return {
            indexKey: 1, // 面包屑
            importConference: false, // 是否显示导入框
            ifErr: false, // 是否展示错误信息
            uploadConfig: {
                fileList: [],
                data: { versionCode: '' },
            },
            applyForm: {
                attachFile: [],
                attachName: []
            },
            fileList: [],
            jsId: '', // 竞赛ID
            jsTitle: '', // 竞赛标题
            isArrange: '', // 是否已经排考
            examType: '', // 排考类型
            courseCode: '',
            fileFlag: true,
            title: '',
            compile: '',
            dataListObj: {},
            editInfo: {},
            category: { category: 1 },
            addBtn: true,
            editBtn: true,
            auths: [],
            addflag: false,
            ifDetials: false,
            copyDialog: false,
            curCalendarId: '',
            exportButton: false,
            exportResult: null,
            dataList: [],
            multipleSelection: [],
            provideData: {}, // 提供子组件表格数据
            queryForm: {
                pageNum_: 1,
                pageSize_: 20,
                condition: {
                    projectName: '',
                    year: new Date().getFullYear() + ''
                }
            },
            // 导入
            exportForm: {},
            // 导入失败列表
            errList: [],
            dialogForm: {
                file: ''
            },
            dataListProp: {},
            dataObjList: [],
            dataObjListTwo: [],
            dataObj: {},
            mark: '',
            copyTime: {
                time: ''
            },
            options: {
                facultys: [],
                timeList: [],
                title: [],
                sex: [],
                regionList: []
            },
            pickerOptionsStart: {
                disabledDate: (time) => {
                    let endDateVal = this.queryForm.condition.signStartStr
                    if (endDateVal) {
                        return time.getTime() > new Date(endDateVal).getTime()
                    }
                }
            },
            pickerOptionsEnd: {
                disabledDate: (time) => {
                    let beginDateVal = this.queryForm.condition.signEndStr
                    if (beginDateVal) {
                        return time.getTime() <= new Date(beginDateVal).getTime() - 86400000
                    }
                }
            },
            queryList: {
                calendarId: '',
                faculty: '',
                status: '',
                course: ''
            },
            id: '',
            formsLearn: [],
            breadPath: {
                label: this.$t('graduate.graduate'),
                key: '1',
                children: [
                    {
                        key: '2',
                        label: ''
                    }
                ]
            }
        }
    },
    watch: {
        auths: {
            handler: function (newVal) {
                if (newVal && Array.isArray(newVal) && newVal.length > 0) {
                    newVal.some((item) => {
                        if (item.authCode === 'academicForums-addBtn') {
                            // 新增
                            this.addBtn = true
                        }
                        if (item.authCode === 'academicForums-editBtn') {
                            // 编辑
                            this.editBtn = true
                        }
                        if (item.authCode === 'internshipDemand-detailsBtn') {
                            // 详情
                            this.detailsBtn = true
                        }
                        if (item.authCode === 'internshipDemand-deriveBtn') {
                            // 导出
                            this.deriveBtn = true
                        }
                        if (item.authCode === 'internshipDemand-removeBtn') {
                            // 删除
                            this.removeBtn = true
                        }
                    })
                }
            },
            deep: true,
            immediate: true
        }
    },
    created() {
        this.postApproveAgree(this.queryForm)
        if (localStorage.getItem('sessiondata')) {
            if (JSON.parse(localStorage.getItem('sessiondata')).auths) {
                this.auths = JSON.parse(localStorage.getItem('sessiondata')).auths
            }
        }
    },
    filters: {},
    mounted() {
        this.getOptions()
    },

    methods: {
        // 排考
        detailsOutline(row) {
            this.courseCode = row.courseCode
            this.jsTitle = row.projectName // 竞赛标题
            this.jsId = row.id // 竞赛ID
            this.isArrange = row.arrangeFinish // 是否已经排考
            this.examType = row.type
            // 获取已报名人数和已排考人数
            competitionRuleApi.getSignedAndArrangedNum(row.id).then((res) => {
                this.mark = res.code
                if (res.code === 200) {
                    this.compile = 'arrange'
                    this.dataObjList = res.data
                    this.indexKey = 6
                }
            })
            // 获取每个教室已安排的学生数
            // competitionRuleApi.getArrangeClassroomInfo(row.id).then(res => {
            //     if (res.code === 200) {
            //         this.compile = 'arrange'
            //         this.dataObjListTwo = res.data
            //         this.indexKey = 6
            //     }
            // })
            // 获取转换后的参数：调用获取空闲教室接口前，先调用该接口，获得调用空闲教室接口的入参
            // competitionRuleApi.getReverseCondition(row.id).then(res => {
            //     this.mark = res.code
            //     if (res.code === 200) {
            //         this.compile = 'arrange'
            //         this.dataObj = res.data
            //         this.indexKey = 6
            //     }
            // })
        },
        // 成绩管理
        achievement(row) {
            // if (row.arrangeFinish !== null) {
                
            // } else {
            //     return this.$message.error(this.$t('registrationList.noExamSchedule'))
            // }
            this.indexKey = 5
            this.dataListObj = row
        },

        // 新增
        exportAdd() {
            this.compile = '新增'
            this.indexKey = 2
        },
        // 新增保存
        changeAdd(data) {
            comRegistration.addCompetition(data).then((res) => {
                this.$message.success(this.$t('internship.successfully'))
                this.$refs.addFinom.cancelClose()
                this.indexKey = 1
                this.queryForm.pageNum_ = 1
                this.postApproveAgree(this.queryForm)
            })
        },
        // 编辑
        editOutline(data) {
            comRegistration.getDetail(data.id).then((res) => {
                this.compile = '编辑'
                this.editInfo = _.cloneDeep(res.data)
                this.indexKey = 3
            })
        },

        // 详情
        viewCompetition(data) {
            // console.log(data,'data');
            comRegistration.getDetail(data.id).then((res) => {
                this.dataListObj = res.data
                this.indexKey = 4
                this.compile = '查看'
            })
        },
        // 查询列表
        postApproveAgree(date) {
            comRegistration.listPage(date).then((res) => {
                this.queryForm.total_ = res.data.total_
                this.dataList = res.data.list
            })
        },
        getOptions() {
            // 筛选条件字典查询
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: ['profession', 'G_XBIE']
            }
            queryDic(data).then((res) => {
                UTILS.fillSelect(
                    {
                        title: 'profession',
                        sex: 'G_XBIE',
                        nationId: 'G_GJQU' // 国别/地区
                    },
                    res.data,
                    this.options
                )
            })
        },
        changeClick(val) {
            this.options.title.forEach((el) => {
                if (val === el.value) {
                    this.queryForm.condition.proTitle = el.label
                }
            })
        },

        // 导出
        exportFile() {
            let param = {
                "records": this.multipleSelection,
                "condition": this.queryForm.condition
            }
            comRegistration.exportList(param)
        },
        // 搜索/筛选
        onSubmit() {
            this.queryForm.pageNum_ = 1
            this.postApproveAgree(this.queryForm)
        },
        handleSelectionChange(val) {
            this.multipleSelection = val
        },

        // 重置
        reset() {
            for (let key in this.queryForm.condition) {
                this.queryForm.condition[key] = ''
            }
            this.fileList = []
            this.queryForm.pageNum_ = 1
            this.postApproveAgree(this.queryForm)
        },
        // 保存
        conserve() {
            this.addflag = false
        },
        // 打印签到表
        printSignInForm(row) {
            if (row.arrangeFinish !== null) {
                let sheet = {
                    competitionId: row.id,
                    classrooms: []
                }
                competitionRuleApi.printArriveSheet(sheet).then((res) => {
                    this.$message.success(this.$t('internship.successfully'))
                })
            } else {
                return this.$message.error(this.$t('registrationList.noExamSchedule'))
            }
        },
        // 考生信息表
        candidateInformationForm(row) {
            if (row.arrangeFinish !== null) {
                let info = {
                    competitionId: row.id,
                    classrooms: []
                }
                competitionRuleApi.printStudentInfo(info).then((res) => {
                    this.$message.success(this.$t('internship.successfully'))
                })
            } else {
                return this.$message.error(this.$t('registrationList.noExamSchedule'))
            }
        },
        // 打印准考证
        printTheAdmissionCard(row) {
            if (row.arrangeFinish !== null) {
                let classrooms = []
                let card = {
                    competitionId: row.id,
                    classrooms: classrooms
                }
                competitionRuleApi.printExamCard(card).then((res) => {
                    this.$message.success(this.$t('internship.successfully'))
                })
            } else {
                return this.$message.error(this.$t('registrationList.noExamSchedule'))
            }
        },
        // 返回主页
        changeExam() {
            this.indexKey = 1
            this.multipleSelection = []
            this.queryForm.pageNum_ = 1
            this.postApproveAgree(this.queryForm)
        },
        handleSizeChange(val) {
            this.queryForm.pageSize_ = val
            this.queryForm.pageNum_ = 1
            this.postApproveAgree(this.queryForm)
        },
        handleCurrentChange(val) {
            this.queryForm.pageNum_ = val
            this.postApproveAgree(this.queryForm)
        },
    },
}
</script>
<style lang="scss" scoped>
.condition-title {
    color: #2b3b4e;
    font-size: 18px;
    line-height: 24px;
    font-weight: bold;
}
.edu-page-title {
    span {
        line-height: 24px;
        display: inline-block;
        padding-right: 10px;
        font-size: 18px;
        font-weight: bold;
        color: #2b3b4e;
    }
}
.sub-title {
    margin: 16px 0;
    font-size: 16px;
    color: #000020;
    letter-spacing: 0;
}
.form-item {
    width: 312px;
    height: 32px;
    float: left;
    margin: 0px 10px;
}
.is-enabled-container ::v-deep {
    .el-button {
        cursor: auto;
    }
    .el-button--danger {
        padding: 9px 15px;
        min-width: 0;
    }
}
.inputBox {
    width: 240px !important;
    height: 32px !important;
}
.atooltip {
    background: beige;
    border: black;
}
</style>
